﻿/*
将以下内容保存为 EntLib.Config 至App或者Web的根目录

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <configSections>
        <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <cachingConfiguration defaultCacheManager="Cache Manager">
        <cacheManagers>
            <add name="Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
                numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
        </cacheManagers>
        <backingStores>
            <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="NullBackingStore" />
        </backingStores>
    </cachingConfiguration>
</configuration>

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using System.ComponentModel.Composition;
using Framework.Common.Cache;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
namespace Framework.Cache.EnterpriseLibrary
{
    /// <summary>
    /// enterprise library cache 
    /// </summary>
    [Export(typeof(ICache))]
    public class EnterpiseLibraryCache : ICache
    {
        private ICacheManager cache;
        /// <summary>
        /// constructor
        /// </summary>
        public EnterpiseLibraryCache()
        {
            FileConfigurationSource fileConfig = new FileConfigurationSource(Common.PathHelper.MapPath("~/entlib.config"));
            CacheManagerFactory cmf = new CacheManagerFactory(fileConfig);
            cache = cmf.CreateDefault();
        }

        /// <summary>
        /// get the count of cache in cacher
        /// </summary>
        public int Count
        {
            get
            {
                return cache.Count;
            }
        }

        /// <summary>
        /// index of the cacher instance
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object this[string key]
        {
            get
            {
                return cache[key];
            }
        }
         
        /// <summary>
        /// add object to cache with no expired time (never expired)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Add(string key, object value)
        {
            cache.Add(key, value);
            return true;
        }
    
        /// <summary>
        /// add object to cache with expired time
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expiredTime"></param>
        /// <returns></returns>
        public bool Add(string key, object value, DateTime expiredTime)
        {
            cache.Add(key, value, CacheItemPriority.Normal, null, new AbsoluteTime(expiredTime));
            return true;
        }
        /// <summary>
        /// add object to cache 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="scavengingPriority"></param>
        /// <param name="refreshAction"></param>
        /// <param name="expirations"></param>
        public void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)
        {
            cache.Add(key, value, scavengingPriority, refreshAction, expirations);
        }
        
        /// <summary>
        /// judge whether contains key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Contains(string key)
        {
            return cache.Contains(key);
        }
        
        /// <summary>
        /// remove all cache
        /// </summary>
        public void Flush()
        {
            cache.Flush();
        }
       
        /// <summary>
        /// get object
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetData(string key)
        {
            return cache.GetData(key);
        }

       
        /// <summary>
        /// get object to generic type 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetData<T>(string key)
        {
            var obj = cache.GetData(key);
            if (obj is T)
            {
                return (T)obj;
            }
            return default(T);
        }
        /// <summary>
        /// remove cache object by key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Remove(string key)
        {
            cache.Remove(key);
            return true;
        }
    }
}
